package com.redlion;

import java.util.ArrayList;

import org.andengine.engine.camera.SmoothCamera;
import org.andengine.engine.handler.IUpdateHandler;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.IOnAreaTouchListener;
import org.andengine.entity.scene.ITouchArea;
import org.andengine.entity.scene.Scene;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.input.sensor.acceleration.AccelerationData;
import org.andengine.input.sensor.acceleration.IAccelerationListener;
import org.andengine.input.touch.TouchEvent;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.debug.Debug;

import android.util.DisplayMetrics;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.redlion.hud.OnCatalogItemClickListener;
import com.redlion.hud.MyHUD.SLOT;
import com.redlion.weapon.Upgrade;
import com.redlion.weapon.WeaponEntry;

public class GameActivity extends SimpleBaseGameActivity implements IAccelerationListener, IOnAreaTouchListener,
		OnCatalogItemClickListener, IUpdateHandler {
	private GameScene currentScene;
	public SmoothCamera mZoomCamera;
	public static final int SCENE_GAME = 0;
	private static final int SCENE_STARTSCREEN = 1;
	private static final int SIMPLE_GAME = 2;
	private static final FixtureDef FIXTURE_DEF = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);

	public static ArrayList<UpdateTask> tasks = new ArrayList<UpdateTask>();

	@Override
	public EngineOptions onCreateEngineOptions() {
		DisplayMetrics metrics = new DisplayMetrics();
		
		Game.getInstance().registerGame(this);
		mZoomCamera = new SmoothCamera(0, 0, Game.CAMERA_WIDTH, Game.CAMERA_HEIGHT, 100, 100, 1f);
		return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(Game.CAMERA_WIDTH, Game.CAMERA_HEIGHT),
				mZoomCamera);
	}

	@Override
	public void onCreateResources() {
		Game.getInstance().registerSceneManager(this);
	}

	public void loadScene(int SCENE) {
		switch (SCENE) {
		case SCENE_GAME:
			currentScene = new GameScene(this);
			mEngine.setScene(currentScene.onCreateScene());
			break;
		case SCENE_STARTSCREEN:
			StartScene c = new StartScene(this);
			mEngine.setScene(c.onCreateScene());
			break;
		case SIMPLE_GAME:
			SimpleGame sg = new SimpleGame(this);
			mEngine.setScene(sg.onCreateScene());
			break;
		default:
			break;
		}
	}

	@Override
	protected Scene onCreateScene() {
		loadScene(SIMPLE_GAME);
		return mEngine.getScene();
	}

	@Override
	public boolean onAreaTouched(TouchEvent pSceneTouchEvent, ITouchArea pTouchArea, float pTouchAreaLocalX, float pTouchAreaLocalY) {
		Debug.d("Temp", "onAreaTouched");
		return false;
	}

	@Override
	public void onAccelerationAccuracyChanged(AccelerationData pAccelerationData) {

	}

	@Override
	public void onAccelerationChanged(AccelerationData pAccelerationData) {

	}

	@Override
	public void onCatalogItemClick(SLOT s, WeaponEntry e, Upgrade u) {
	}

	@Override
	public void onUpdate(float pSecondsElapsed) {
		for (UpdateTask task : tasks) {
			task.run();
		}
		tasks.clear();
	}

	public static void registerUpdateTask(UpdateTask task) {
		tasks.add(task);
	}

	@Override
	public void reset() {

	}

	@Override
	protected void onDestroy() {
		Game.saveData(getApplicationContext());
		super.onDestroy();
	}
}
